import express, { Router } from 'express';
import { LessonPlanController } from '../controllers/LessonPlanController';
import { authMiddleware } from '../middleware/auth';
import {
  validateCreateLessonPlan,
  validateUpdateLessonPlan,
  validateDraftLessonPlan,
  validateSearchLessonPlan,
  validateGetLessonPlanList,
  validateObjectId
} from '../middleware/lessonPlanValidation';

const router: Router = express.Router();
const lessonPlanController = new LessonPlanController();

// 搜索教案 (必须在动态路由之前)
router.get('/search', validateSearchLessonPlan, lessonPlanController.search);

// 获取热门搜索
router.get('/hot-searches', lessonPlanController.getHotSearches);

// 获取分类统计
router.get('/category-stats', lessonPlanController.getCategoryStats);

// 获取我的教案列表 (需要认证)
router.get('/my', authMiddleware, validateGetLessonPlanList, lessonPlanController.getMyLessonPlans);

// 获取教案列表
router.get('/', validateGetLessonPlanList, lessonPlanController.getAll);

// 获取单个教案
router.get('/:id', validateObjectId, lessonPlanController.getById);

// 创建教案 (需要认证)
router.post('/', authMiddleware, validateCreateLessonPlan, lessonPlanController.create);

// 保存草稿 (需要认证)
router.post('/draft', authMiddleware, validateDraftLessonPlan, lessonPlanController.saveDraft);

// 更新草稿 (需要认证)
router.put('/:id/draft', authMiddleware, validateObjectId, validateDraftLessonPlan, lessonPlanController.updateDraft);

// 发布教案 (需要认证)
router.put('/:id/publish', authMiddleware, validateObjectId, lessonPlanController.publish);

// 归档教案 (需要认证)
router.put('/:id/archive', authMiddleware, validateObjectId, lessonPlanController.archive);

// 切换点赞状态 (需要认证)
router.post('/:id/toggle-like', authMiddleware, validateObjectId, lessonPlanController.toggleLike);

// 更新教案 (需要认证)
router.put('/:id', authMiddleware, validateObjectId, validateUpdateLessonPlan, lessonPlanController.update);

// 获取教案版本历史
router.get('/:id/versions', validateObjectId, lessonPlanController.getVersionHistory);

// 获取特定版本的教案
router.get('/:id/versions/:version', validateObjectId, lessonPlanController.getSpecificVersion);

// 恢复到指定版本 (需要认证)
router.put('/:id/versions/:version/restore', authMiddleware, validateObjectId, lessonPlanController.restoreVersion);

// 删除教案 (需要认证)
router.delete('/:id', authMiddleware, validateObjectId, lessonPlanController.delete);

export default router;